package com.woniuxy.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import com.woniuxy.entity.Orders;
import com.woniuxy.entity.Perms;
import com.woniuxy.entity.Roles;
import com.woniuxy.entity.User;

@Mapper
public interface OrdersMapper {
	// 查询所有订单
	@Select("select * from orders")
	public List<Orders> findAllOrders();

	// 按用户id查询订单中商品
	@Select("select * from orders where u_id=#{u_id}")
	@Results({ @Result(id = true, column = "u_id", property = "u_id"),
			@Result(property = "product", column = "p_id", one = @One(select = "com.woniuxy.mapper.ProductMapper.findProductByPid", fetchType = FetchType.EAGER)) })
	public List<Orders> findOrdersByUid(int u_id);

	// 按状态查询
	@Select("select * from orders where status=#{status}")
	public List<Orders> findOrdersByStatus(int status);

	// 按订单号查询
	@Select("select * from orders where order_no=#{order_no}")
	public Orders findOrdersByOn(String order_no);

	// 查询昨天交易额
	@Select("SELECT * FROM orders WHERE TO_DAYS( NOW( ) ) - TO_DAYS(remaining_time) <= 1 ")
	public List<Orders> findYesterday();

	// 查询某天交易额
	@Select("select * from orders where ( datediff ( remaining_time , #{time} ) = 0 )")
	public List<Orders> findSomeday(Date time);

	// 查询时间段交易额
	@Select("SELECT * FROM orders WHERE remaining_time >= #{min_time} AND remaining_time  <= #{max_time}")
	public List<Orders> findTimeFrame(Date max_time, Date min_time);

	// 修改订单状态
	@Update("update orders set status=#{status} where order_no=#{order_no}")
	public int updataStatus(String order_no, int status);

	// 修改订单信息
	@Update("update orders set remaining_time=#{remaining_time},Phone=#{Phone},receiver_address=#{receiver_address},consignee=#{consignee} where o_id=#{o_id}")
	public int updataOrder(Orders order);

	// 修改生成时间
	@Update("update orders set order_date=#{order_date} where o_id=#{o_id}")
	public int updataDate(int o_id, Date order_date);

	// 修改付款时间
	@Update("update orders set remaining_time=#{remaining_time},status='1' where o_id=#{o_id}")
	public int updataTime(int o_id, Date remaining_time);

	@Insert("insert into orders values(default,#{p_id},#{u_id},#{order_no},#{order_date},#{auction},#{service_charge},#{total_money},#{consignee},#{receiver_address},#{phone},#{remaining_time}),#{status})")
	public int addOrders(Orders order);

	//
	@Delete("delete from orders where o_id=#{o_id}")
	public int delOrders(int o_id);

}
